/*
Exercise 2.2 - Digital Thermometer
 Created 15/04/2010 ---  By John Boxall --- http://tronixstuff.wordpress.com ---  CC by-sa v3.0 
 
 Task: Digital thermometer with current temperature display, resettable minimum and maximum
 Works in C and F
 
 */

#include <LiquidCrystal.h> // we need this library for the LCD commands

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

// define variables
float currentC = 0;
float currentF = 0;
float minC = 0;
float maxC = 0;
float minF = 0;
float maxF = 0;
float voltage = 0;
float sensor = 0;
int pressed = 0; 
int pressed2 = 0;
int action = 0;


void setup() 
{
  lcd.begin(16, 2);               // need to specify how many columns and rows are in the LCD unit
  lcd.println("tronixstuff FTW!  "); // narcissism
  delay(3000);
  lcd.clear();
  // need to get temperature to initialise min/max
  sensor = analogRead(5);       // TMP36 sensor output pin is connected to Arduino analogue pin 0
  voltage = (sensor*5000)/1024; // convert raw sensor value to millivolts
  voltage = voltage-500;        // remove voltage offset
  currentC = voltage/10;         // convert millivolts to Celsius
  currentF = ((currentC*1.8)+32); // convert c to f
  minC = currentC;
  maxC = currentC;
  minF = currentF;
  maxF = currentF;
  // set up buttons on digital inputs
  pinMode (2, INPUT);
  pinMode (3, INPUT);
}

void loop() 
{
  // read current temperature
  sensor = analogRead(5);       // TMP36 sensor output pin is connected to Arduino analogue pin 5
  voltage = (sensor*5000)/1024; // convert raw sensor value to millivolts
  voltage = voltage-500;        // remove voltage offset
  currentC = voltage/10;         // convert millivolts to Celsius
  currentF = ((currentC*1.8)+32); // convert c to f
  if (currentC<minC) 
  {
    minC = currentC;
  }
  if (currentC>maxC) 
  {
    maxC = currentC;
  }
  if (currentF<minF)
  {
    minF = currentF;
  }
  if (currentF>maxF)
  {
    maxF = currentF;
  }

  // display current temperature
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Current temp:");
  lcd.setCursor(0,1);
  lcd.print(currentC,1);
  lcd.print(" C/");
  lcd.print(currentF,1);
  lcd.print(" F");
  delay(100);

  // has the user pressed reset button?

  pressed = digitalRead(2);
  if (pressed == HIGH)
  {
    pressed = 0;
    action = 0;
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Reset min/max?");
    lcd.setCursor(0,1);
    lcd.print("1 = Yes, 2 = No");
    delay(500); // gives time to ignore switch bounce
    while (action == 0) // loop around until the user presses yes or no for reset 
    {
      if (digitalRead(2)==HIGH)  // check yes button
      {
        minC = currentC;
        maxC = currentC;
        minF = currentF;
        maxF = currentF;
        action = 1;// get me out of this while loop and back to normal operation
        delay(500); // gives time to ignore switch bounce
      }
      if (digitalRead(3)==HIGH) // check no button
      {
        action = 1; // get me out of this while loop and back to normal operation
        delay(500); // gives time to ignore switch bounce
      }

    } 
    
  }


  // has the user pressed min/max button?

  pressed = digitalRead(3);
  if (pressed == HIGH)
  {
    pressed = 0;
    // display minimum temperature
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Minimum temp:");
    lcd.setCursor(0,1);
    lcd.print(minC,1);
    lcd.print(" C/");
    lcd.print(minF,1);
    lcd.print(" F");
    delay(3000); // wait

    // display maximum temperature
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Maximum temp:");
    lcd.setCursor(0,1);
    lcd.print(maxC,1);
    lcd.print(" C/");
    lcd.print(maxF,1);
    lcd.print(" F");
    delay(3000); // wait 
  }
}




